
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第三节 滑动平均 · Tensorflow学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="scottdu">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="section2.4.html" />
    
    
    <link rel="prev" href="section2.2.html" />
    

    
    <link rel="stylesheet" href="../gitbook/gitbook-plugin-chart/c3/c3.min.css">
    <script src="../gitbook/gitbook-plugin-chart/c3/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-chart/c3/c3.min.js"></script>
    

    <script src="../gitbook/gitbook-plugin-graph/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-graph/function-plot.js"></script>    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter1/">
            
                <a href="../chapter1/">
            
                    
                    第一章 Tensorflow框架
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/section1.1.html">
            
                <a href="../chapter1/section1.1.html">
            
                    
                    第一节 张量、计算图、会话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/section1.2.html">
            
                <a href="../chapter1/section1.2.html">
            
                    
                    第二节 前向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/section1.3.html">
            
                <a href="../chapter1/section1.3.html">
            
                    
                    第三节 反向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/section1.4.html">
            
                <a href="../chapter1/section1.4.html">
            
                    
                    第四节 搭建神经网络的步骤
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    第二章 神经网络优化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="section2.1.html">
            
                <a href="section2.1.html">
            
                    
                    第一节 损失函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="section2.2.html">
            
                <a href="section2.2.html">
            
                    
                    第二节 学习率
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.3" data-path="section2.3.html">
            
                <a href="section2.3.html">
            
                    
                    第三节 滑动平均
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="section2.4.html">
            
                <a href="section2.4.html">
            
                    
                    第四节 正则化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="section2.5.html">
            
                <a href="section2.5.html">
            
                    
                    第五节 神经网络的搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter3/">
            
                <a href="../chapter3/">
            
                    
                    第三章 全连接网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/section3.1.html">
            
                <a href="../chapter3/section3.1.html">
            
                    
                    第一节 MINIST数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/section3.2.html">
            
                <a href="../chapter3/section3.2.html">
            
                    
                    第二节 模块化搭建神经网络方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/section3.3.html">
            
                <a href="../chapter3/section3.3.html">
            
                    
                    第三节 手写数字识别准确率输出
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter4/">
            
                <a href="../chapter4/">
            
                    
                    第四章 全连接网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter4/section4.1.html">
            
                <a href="../chapter4/section4.1.html">
            
                    
                    第一节 输入手写数字图片输出识别结果
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter4/section4.2.html">
            
                <a href="../chapter4/section4.2.html">
            
                    
                    第二节 制作数据集
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    第五章 卷积网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter5/section5.1.html">
            
                <a href="../chapter5/section5.1.html">
            
                    
                    第一节 卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter5/section5.2.html">
            
                <a href="../chapter5/section5.2.html">
            
                    
                    第二节 lenel5代码讲解
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter6/">
            
                <a href="../chapter6/">
            
                    
                    第六章 卷积网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/section6.1.html">
            
                <a href="../chapter5/section6.1.html">
            
                    
                    第一节 复现已有的卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter6/section6.2.html">
            
                <a href="../chapter6/section6.2.html">
            
                    
                    第二节 用vgg16实现图片识别
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter7/">
            
                <a href="../chapter7/">
            
                    
                    第七章 Tensorflow应用
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >第三节 滑动平均</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7B2C;&#x4E09;&#x8282;-&#x6ED1;&#x52A8;&#x5E73;&#x5747;">&#x7B2C;&#x4E09;&#x8282; &#x6ED1;&#x52A8;&#x5E73;&#x5747;</h1>
<ul>
<li>&#x6ED1;&#x52A8;&#x5E73;&#x5747;:&#x8BB0;&#x5F55;&#x4E86;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x5185;&#x6A21;&#x578B;&#x4E2D;&#x6240;&#x6709;&#x53C2;&#x6570;<code>w</code>&#x548C;<code>b</code>&#x5404;&#x81EA;&#x7684;&#x5E73;&#x5747;&#x503C;&#x3002;&#x5229;&#x7528;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;&#x53EF;&#x4EE5;&#x589E;&#x5F3A;&#x6A21;&#x578B;&#x7684;&#x6CDB;&#x5316;&#x80FD;&#x529B;&#x3002;</li>
<li>&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;(&#x5F71;&#x5B50;)&#x8BA1;&#x7B97;&#x516C;&#x5F0F;:<code>&#x5F71;&#x5B50;=&#x8870;&#x51CF;&#x7387;*&#x5F71;&#x5B50;+(1-&#x8870;&#x51CF;&#x7387;)*&#x53C2;&#x6570;</code>;&#x5176;&#x4E2D;&#xFF0C;</li>
</ul>
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>D</mi><mi>E</mi><mi>C</mi><mi>A</mi><mi>Y</mi><mi mathvariant="normal">_</mi><mi>R</mi><mi>A</mi><mi>T</mi><mi>E</mi><mo>=</mo><mi>m</mi><mi>i</mi><mi>n</mi><mo>{</mo><mi>M</mi><mi>O</mi><mi>V</mi><mi>I</mi><mi>N</mi><msub><mi>G</mi><mrow><mi>A</mi><mi>V</mi><mi>E</mi><mi>R</mi><mi>A</mi><mi>G</mi><msub><mi>E</mi><mrow><mi>D</mi><mi>E</mi><mi>C</mi><mi>A</mi><msup><mi>Y</mi><mrow><mi mathvariant="normal">&#x2032;</mi></mrow></msup></mrow></msub></mrow></msub><mfrac><mrow><mn>1</mn><mo>+</mo><mi>S</mi><mi>T</mi><mi>E</mi><mi>P</mi><mi>S</mi></mrow><mrow><mn>1</mn><mn>0</mn><mo>+</mo><mi>S</mi><mi>T</mi><mi>E</mi><mi>P</mi><mi>S</mi></mrow></mfrac><mo>}</mo></mrow><annotation encoding="application/x-tex">DECAY\_RATE=min\{MOVING_{AVERAGE_{DECAY&apos;}}\frac{1+STEPS}{10+STEPS}\}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.872331em;"></span><span class="strut bottom" style="height:1.275662em;vertical-align:-0.403331em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.02778em;">D</span><span class="mord mathit" style="margin-right:0.05764em;">E</span><span class="mord mathit" style="margin-right:0.07153em;">C</span><span class="mord mathit">A</span><span class="mord mathit" style="margin-right:0.22222em;">Y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathit" style="margin-right:0.00773em;">R</span><span class="mord mathit">A</span><span class="mord mathit" style="margin-right:0.13889em;">T</span><span class="mord mathit" style="margin-right:0.05764em;">E</span><span class="mrel">=</span><span class="mord mathit">m</span><span class="mord mathit">i</span><span class="mord mathit">n</span><span class="mopen">{</span><span class="mord mathit" style="margin-right:0.10903em;">M</span><span class="mord mathit" style="margin-right:0.02778em;">O</span><span class="mord mathit" style="margin-right:0.22222em;">V</span><span class="mord mathit" style="margin-right:0.07847em;">I</span><span class="mord mathit" style="margin-right:0.10903em;">N</span><span class="mord"><span class="mord mathit">G</span><span class="msupsub"><span class="vlist"><span style="top:0.15000000000000002em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathit mtight">A</span><span class="mord mathit mtight" style="margin-right:0.22222em;">V</span><span class="mord mathit mtight" style="margin-right:0.05764em;">E</span><span class="mord mathit mtight" style="margin-right:0.00773em;">R</span><span class="mord mathit mtight">A</span><span class="mord mathit mtight">G</span><span class="mord mtight"><span class="mord mathit mtight" style="margin-right:0.05764em;">E</span><span class="msupsub"><span class="vlist"><span style="top:0.2620285714285715em;margin-right:0.07142857142857144em;margin-left:-0.05764em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptstyle scriptscriptstyle cramped mtight"><span class="mord scriptscriptstyle cramped mtight"><span class="mord mathit mtight" style="margin-right:0.02778em;">D</span><span class="mord mathit mtight" style="margin-right:0.05764em;">E</span><span class="mord mathit mtight" style="margin-right:0.07153em;">C</span><span class="mord mathit mtight">A</span><span class="mord mtight"><span class="mord mathit mtight" style="margin-right:0.22222em;">Y</span><span class="msupsub"><span class="vlist"><span style="top:-0.294em;margin-right:0.1em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-scriptscriptstyle scriptscriptstyle cramped mtight"><span class="mord scriptscriptstyle cramped mtight"><span class="mord mathrm mtight">&#x2032;</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mord reset-textstyle textstyle uncramped"><span class="mopen sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span><span class="mfrac"><span class="vlist"><span style="top:0.345em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span><span class="mord mathrm mtight">0</span><span class="mbin mtight">+</span><span class="mord mathit mtight" style="margin-right:0.05764em;">S</span><span class="mord mathit mtight" style="margin-right:0.13889em;">T</span><span class="mord mathit mtight" style="margin-right:0.05764em;">E</span><span class="mord mathit mtight" style="margin-right:0.13889em;">P</span><span class="mord mathit mtight" style="margin-right:0.05764em;">S</span></span></span></span><span style="top:-0.22999999999999998em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle textstyle uncramped frac-line"></span></span><span style="top:-0.394em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped mtight"><span class="mord scriptstyle uncramped mtight"><span class="mord mathrm mtight">1</span><span class="mbin mtight">+</span><span class="mord mathit mtight" style="margin-right:0.05764em;">S</span><span class="mord mathit mtight" style="margin-right:0.13889em;">T</span><span class="mord mathit mtight" style="margin-right:0.05764em;">E</span><span class="mord mathit mtight" style="margin-right:0.13889em;">P</span><span class="mord mathit mtight" style="margin-right:0.05764em;">S</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span><span class="mclose sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span><span class="mclose">}</span></span></span></span></p>
<p>&#x5F71;&#x5B50;&#x521D;&#x503C;=&#x53C2;&#x6570;&#x521D;&#x503C;</p>
<ul>
<li>&#x7528;Tensorflow&#x51FD;&#x6570;&#x8868;&#x793A;&#x4E3A;&#xFF1A;</li>
</ul>
<pre><code>ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, gloabl_step)
</code></pre><p>&#x5176;&#x4E2D;&#xFF0C;<code>MOVING_AVERAGE_DECAY</code>&#x8868;&#x793A;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x8870;&#x51CF;&#x7387;&#xFF0C;&#x4E00;&#x822C;&#x4F1A;&#x8D4B;&#x63A5;&#x8FD1;1&#x7684;&#x503C;&#xFF0C;<code>global_step</code>&#x8868;&#x793A;&#x5F53;&#x524D;&#x8BAD;&#x7EC3;&#x4E86;&#x591A;&#x5C11;&#x8F6E;&#x3002;</p>
<pre><code>ema_op = ema.apply(tf.trainable_variables())
</code></pre><p>&#x5176;&#x4E2D;&#xFF0C;<code>ema.apply()</code>&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x5BF9;&#x62EC;&#x53F7;&#x5185;&#x53C2;&#x6570;&#x6C42;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#xFF0C;<code>tf.trainable_variables()</code>&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x628A;&#x6240;&#x6709; &#x5F85;&#x8BAD;&#x7EC3;&#x53C2;&#x6570;&#x6C47;&#x603B;&#x4E3A;&#x5217;&#x8868;&#x3002;</p>
<pre><code>  train_op = tf.no_op(name=&apos;train&apos;)_step, ema_op]):
</code></pre><p>&#x5176;&#x4E2D;&#xFF0C;&#x8BE5;&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x5C06;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x548C;&#x8BAD;&#x7EC3;&#x8FC7;&#x7A0B;&#x540C;&#x6B65;&#x8FD0;&#x884C;&#x3002;</p>
<p>&#x67E5;&#x770B;&#x6A21;&#x578B;&#x4E2D;&#x53C2;&#x6570;&#x7684;&#x5E73;&#x5747;&#x503C;&#xFF0C;&#x53EF;&#x4EE5;&#x7528; <code>ema.average()</code>&#x51FD;&#x6570;&#x3002;</p>
<p>&#x5728;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#x4E2D;&#xFF0C;&#x5C06;<code>MOVING_AVERAGE_DECAY</code>&#x8BBE;&#x7F6E;&#x4E3A;0.99&#xFF0C;&#x53C2;&#x6570;<code>w1</code>&#x8BBE;&#x7F6E;&#x4E3A;0&#xFF0C;<code>w1</code>&#x7684;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;&#x8BBE; &#x7F6E;&#x4E3A;0&#x3002;</p>
<p><code>w1</code>&#xFF0C;<code>&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;=min(0.99,1/10)*0+(1&#x2013; min(0.99,1/10)*1 = 0.9</code>&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;&#x4E3A;:</p>
<p>(2)&#x5F53;&#x8F6E;&#x6570;<code>global_step</code>&#x8BBE;&#x7F6E;&#x4E3A;100&#x65F6;&#xFF0C;&#x53C2;&#x6570;<code>w1</code>&#x66F4;&#x65B0;&#x4E3A;10&#xFF0C;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;<code>global_step</code>&#x4FDD;&#x6301;&#x4E3A;100&#xFF0C;&#x6BCF;&#x6B21;&#x6267;&#x884C;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x64CD;&#x4F5C;&#x5F71;&#x5B50;&#x503C;&#x66F4;&#x65B0;&#xFF0C;&#x5219;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;&#x53D8;&#x4E3A;:<code>w1&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;=min(0.99,101/110)*0.9+(1&#x2013; min(0.99,101/110)*10 = 0.826+0.818=1.644</code></p>
<p>(3)&#x518D;&#x6B21;&#x8FD0;&#x884C;&#xFF0C;&#x53C2;&#x6570;<code>w1</code>&#x66F4;&#x65B0;&#x4E3A;1.644&#xFF0C;&#x5219;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;&#x53D8;&#x4E3A;:<code>w1&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;=min(0.99,101/110)*1.644+(1&#x2013; min(0.99,101/110)*10 = 2.328</code></p>
<p>(4)&#x518D;&#x6B21;&#x8FD0;&#x884C;&#xFF0C;&#x53C2;&#x6570;<code>w1</code>&#x66F4;&#x65B0;&#x4E3A;2.328&#xFF0C;&#x5219;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;:<code>w1&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;=2.956</code></p>
<p>&#x4EE3;&#x7801;&#x5982;&#x4E0B;:</p>
<pre><code>#encoding:utf-8
import tensorflow as tf

#1. &#x5B9A;&#x4E49;&#x53D8;&#x91CF;&#x53CA;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x7C7B;
# &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;32&#x4F4D;&#x6D6E;&#x70B9;&#x53D8;&#x91CF;&#xFF0C;&#x521D;&#x59CB;&#x503C;&#x4E3A;0.0 &#x8FD9;&#x4E2A;&#x4EE3;&#x7801;&#x5C31;&#x662F;&#x4E0D;&#x65AD;&#x66F4;&#x65B0;w1&#x53C2;&#x6570;&#xFF0C;&#x4F18;&#x5316;w1&#x53C2;&#x6570;&#xFF0C;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x505A;&#x4E86;&#x4E2A;w1&#x7684;&#x5F71;&#x5B50;
w1 = tf.Variable(0, dtype=tf.float32)

#&#x5B9A;&#x4E49;num_updates(NN&#x7684;&#x8FED;&#x4EE3;&#x8F6E;&#x6570;)&#xFF0C;&#x521D;&#x59CB;&#x503C;&#x4E3A;0&#xFF0C;&#x4E0D;&#x53EF;&#x88AB;&#x4F18;&#x5316;&#xFF08;&#x8BAD;&#x7EC3;&#xFF09;&#xFF0C;&#x8FD9;&#x4E2A;&#x53C2;&#x6570;&#x4E0D;&#x8BAD;&#x7EC3;
global_step = tf.Variable(0, trainable=False)

#&#x5B9E;&#x4F8B;&#x5316;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x7C7B;&#xFF0C;&#x7ED9;&#x5220;&#x51CF;&#x7387;&#x4E3A;0.99&#xFF0C;&#x5F53;&#x524D;&#x8F6E;global_step
MOVING_AVERAGE_DECAY = 0.99
ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)

#ema.apply&#x540E;&#x7684;&#x62EC;&#x53F7;&#x91CC;&#x662F;&#x66F4;&#x65B0;&#x5217;&#x8868;&#xFF0C;&#x6BCF;&#x6B21;&#x8FD0;&#x884C;sess.run(ema_op)&#x65F6;&#xFF0C;&#x5BF9;&#x66F4;&#x65B0;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#x6C42;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;
#&#x5728;&#x5B9E;&#x9645;&#x5E94;&#x7528;&#x4E2D;&#x4F1A;&#x4F7F;&#x7528;tf.trainable_variables()&#x81EA;&#x52A8;&#x5C06;&#x6240;&#x6709;&#x5F85;&#x8BAD;&#x7EC3;&#x7684;&#x53C2;&#x6570;&#x6C47;&#x603B;&#x4E3A;&#x5217;&#x8868;
#ema_op = ema.apply([w1])
ema_op = ema.apply(tf.trainable_variables())

#2. &#x67E5;&#x770B;&#x4E0D;&#x540C;&#x8FED;&#x4EE3;&#x4E2D;&#x53D8;&#x91CF;&#x53D6;&#x503C;&#x7684;&#x53D8;&#x5316;
with tf.Session() as sess:
  #&#x521D;&#x59CB;&#x5316;
  init_op = tf.global_variables_initializer()
  sess.run(init_op)
  #&#x7528;ema.average(w1)&#x83B7;&#x53D6;w1&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;&#xFF08;&#x8981;&#x8FD0;&#x884C;&#x591A;&#x4E2A;&#x8282;&#x70B9;&#xFF0C;&#x4F5C;&#x4E3A;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#x5217;&#x51FA;&#xFF0C;&#x5199;&#x5728;sess.run&#x4E2D;&#xFF09;
  #&#x6253;&#x5370;&#x51FA;&#x5F53;&#x524D;&#x53C2;&#x6570;w1&#x548C;w1&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;
  print(sess.run([w1, ema.average(w1)]))

  #&#x53C2;&#x6570;w1&#x7684;&#x503C;&#x8D4B;&#x4E3A;1
  sess.run(tf.assign(w1,1))
  sess.run(ema_op)
  print(sess.run([w1,ema.average(w1)]))

  #&#x66F4;&#x65B0;step&#x7684;w1&#x7684;&#x503C;&#xFF0C;&#x6A21;&#x62DF;&#x51FA;100&#x8F6E;&#x8FED;&#x4EE3;&#x540E;&#xFF0C;&#x53C2;&#x6570;w1&#x53D8;&#x4E3A;10
  sess.run(tf.assign(global_step, 100))
  sess.run(tf.assign(w1, 10))
  sess.run(ema_op)
  print(sess.run([w1, ema.average(w1)]))

  #&#x6BCF;&#x6B21;sess.run&#x4F1A;&#x66F4;&#x65B0;&#x4E00;&#x6B21;w1&#x7684;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;
  sess.run(ema_op)
  print(sess.run([w1,ema.average(w1)]))

    sess.run(ema_op)
  print(sess.run([w1,ema.average(w1)]))

  sess.run(ema_op)
  print(sess.run([w1,ema.average(w1)]))

  sess.run(ema_op)
  print(sess.run([w1,ema.average(w1)]))

  sess.run(ema_op)
  print(sess.run([w1,ema.average(w1)]))

  sess.run(ema_op)
  print(sess.run([w1,ema.average(w1)]))
</code></pre><p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>[0.0, 0.0]
[1.0, 0.89999998]
[10.0, 1.6445453]
[10.0, 2.3281732]
[10.0, 2.955868]
[10.0, 3.5322061]
[10.0, 4.061389]
[10.0, 4.5472751]
[10.0, 4.9934072]
</code></pre><p>&#x4ECE;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x53EF;&#x77E5;&#xFF0C;&#x6700;&#x521D;&#x53C2;&#x6570;<code>w1</code>&#x548C;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;&#x90FD;&#x662F;0;&#x53C2;&#x6570;<code>w1</code>&#x8BBE;&#x5B9A;&#x4E3A;1&#x540E;&#xFF0C;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;&#x53D8;&#x4E3A;0.9;&#x5F53;&#x8FED;&#x4EE3;&#x8F6E;&#x6570;&#x66F4;&#x65B0;&#x4E3A;100&#x8F6E;&#x65F6;&#xFF0C;&#x53C2;&#x6570;<code>w1</code>&#x66F4;&#x65B0;&#x4E3A;10&#x540E;&#xFF0C;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;&#x53D8;&#x4E3A;1.644&#x3002;&#x968F;&#x540E;&#x6BCF;&#x6267;&#x884C;&#x4E00;&#x6B21;&#xFF0C;&#x53C2;&#x6570;<code>w1</code>&#x7684;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x503C;&#x90FD;&#x5411;&#x53C2;&#x6570;<code>w1</code>&#x9760;&#x8FD1;&#x3002;&#x53EF;&#x89C1;&#xFF0C;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x8FFD;&#x968F;&#x53C2;&#x6570;&#x7684;&#x53D8;&#x5316;&#x800C;&#x53D8;&#x5316;&#x3002;</p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; scottdu 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2018-08-18 15:53:44
</span></footer> <link rel="stylesheet" type="text/css" href="https://storage.googleapis.com/app.klipse.tech/css/codemirror.css"> <script>     window.klipse_settings = {         selector: ".language-klipse, .lang-eval-clojure",         selector_eval_js: ".lang-eval-js",         selector_eval_python_client: ".lang-eval-python",         selector_eval_php: ".lang-eval-php",         selector_eval_scheme: ".lang-eval-scheme",         selector_eval_ruby: ".lang-eval-ruby",         selector_reagent: ".lang-reagent",        selector_google_charts: ".lang-google-chart",        selector_es2017: ".lang-eval-es2017",        selector_jsx: ".lang-eval-jsx",        selector_transpile_jsx: ".lang-transpile-jsx",        selector_render_jsx: ".lang-render-jsx",        selector_react: ".lang-react",        selector_eval_markdown: ".lang-render-markdown",        selector_eval_lambdaway: ".lang-render-lambdaway",        selector_eval_cpp: ".lang-eval-cpp",        selector_eval_html: ".lang-render-html",        selector_sql: ".lang-eval-sql",        selector_brainfuck: "lang-eval-brainfuck",        selector_js: ".lang-transpile-cljs"    }; </script> <script src="https://storage.googleapis.com/app.klipse.tech/plugin/js/klipse_plugin.js"></script>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="section2.2.html" class="navigation navigation-prev " aria-label="Previous page: 第二节 学习率">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="section2.4.html" class="navigation navigation-next " aria-label="Next page: 第四节 正则化">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第三节 滑动平均","level":"1.3.3","depth":2,"next":{"title":"第四节 正则化","level":"1.3.4","depth":2,"path":"chapter2/section2.4.md","ref":"chapter2/section2.4.md","articles":[]},"previous":{"title":"第二节 学习率","level":"1.3.2","depth":2,"path":"chapter2/section2.2.md","ref":"chapter2/section2.2.md","articles":[]},"dir":"ltr"},"config":{"plugins":["katex","expandable-chapters-small","tbfed-pagefooter","alerts","copy-code-button","puml","graph","chart","klipse","donate","simple-page-toc","splitter"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy scottdu 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"puml":{},"simple-page-toc":{"maxDepth":3,"skipFirstH1":true},"splitter":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"graph":{},"donate":{"alipay":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-alipay1.jpeg","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-wechatpay.png","wechatText":"微信打赏"},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"alerts":{},"expandable-chapters-small":{},"copy-code-button":{},"klipse":{"myConfigKey":"it's the default value"},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"chart":{"type":"c3"}},"theme":"default","author":"scottdu","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Tensorflow学习笔记","language":"zh-hans","gitbook":"3.2.3","description":"记录Tensorflow的学习内容"},"file":{"path":"chapter2/section2.3.md","mtime":"2018-08-18T07:53:44.135Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-25T05:55:22.995Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

